package com.kafka.service;

import java.io.IOException;
import java.util.Properties;

import javax.validation.Valid;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.PropertyConfigurator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.kafka.common.Constant;
import com.kafka.core.BaseProducer;
import com.kafka.enumtype.MsgType;
import com.kafka.enumtype.ResponseType;
import com.kafka.util.AES;
import com.kafka.util.BaseUtil;
import com.kafka.valid.AccountValid;
import com.kafka.valid.DeviceValid;
import com.kafka.valid.PayValid;
import com.kafka.valid.UserValid;

/**
 * 会员消费消息
 * 
 * @author 雷洪岸
 * 
 */
@Path("valid")
public class ValidResource extends BaseProducer{
	private static Logger log = LoggerFactory.getLogger(ValidResource.class);
	// 解密密钥
	private static String skey;

	public ValidResource() {
		Properties properties = new Properties();
		try {
			properties.load(ClassLoader
					.getSystemResourceAsStream("sysconfig.properties"));
			skey = properties.getProperty("sKey");
		} catch (IOException e) {
			e.printStackTrace();
		}
		PropertyConfigurator.configure("config/log4j.properties");
	}

	@POST
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	public String msgCheck(@Valid String message){
		JSONObject jsonMsg = null;
		JSONObject ck = null;
		try {
			// 数据解密
			if (!message.isEmpty()) {
				try {
					message = AES.Decrypt(message, skey);
					log.info("###Decrypt:" + message.toString() + ",time:" + BaseUtil.getDate());
				} catch (Exception e) {
					log.info("###Decrypt:" + "解密数据时发生异常:" + e.toString() + ",time:" + BaseUtil.getDate());
					return failMsg(JSONObject.fromObject(message), "解密数据时发生异常:"+e.toString());
				}
			}
			// 重构数据字段大小写
			jsonMsg = rebuildJSON(JSONObject.fromObject(message));
			if (jsonMsg != null) {
				//重构json数据
				if(MsgType.pay.toString().equals(jsonMsg.getString("msgtype")) || MsgType.RECHARGE.toString().equals(jsonMsg.getString("msgtype"))){
					jsonMsg.put("datas", rebuildJSONArray(jsonMsg.getJSONArray("datas")));
				}else{
					jsonMsg.put("datas", rebuildJSON(jsonMsg.getJSONObject("datas")));
				}
				log.info("###check:" + message.toString() + ",time:" + BaseUtil.getDate());
				ck = validParams(jsonMsg,jsonMsg.getString("msgtype"));
				if(ck != null){
					//校验失败
					return validParams(jsonMsg,jsonMsg.getString("msgtype")).toString();
				}else{
					//校验成功
					return successMsg(jsonMsg.getString("msgid")).toString();
				}	
			}else{
				return failMsg(jsonMsg, "json数据格式不正确或串为空！");
			}
		} catch (Exception e) {
			e.printStackTrace();
			String rs = failMsg(jsonMsg, "json格式错误！"+e.toString());
			return rs;
		}
	}
	
	/**
	 * 校验数据合法性
	 * 
	 * @return
	 */
	public static JSONObject validParams(JSONObject message, String msgType){
		JSONObject params = new JSONObject();
		JSONArray goodsObj = new JSONArray();
		JSONObject result = new JSONObject();
		//消费数据格式与其他类型的数据格式有所区别使用的是JSONArray
		if(MsgType.pay.toString().equals(msgType)){
			goodsObj = rebuildJSONArray(message.getJSONArray("datas"));
		} else {
			params = rebuildJSON(message.getJSONObject("datas"));
		}
		String msgId = message.getString("msgid");
		if (MsgType.pay.toString().equals(msgType)) {
			return PayValid.validPay(goodsObj, msgId);
		} else if (MsgType.REGISTER.toString().equals(msgType)){
			return UserValid.validRegister(params, msgId,Constant.VALID);
		}else if(MsgType.UDATE.toString().equals(msgType)){
			return UserValid.validUpdate(params, msgId);
		}else if(MsgType.ACTIVATEUSER.toString().equals(msgType)){
			return UserValid.validActivate(params, msgId);
		}else if(MsgType.USERBLACKLIST.toString().equals(msgType)){
			return UserValid.validBlackList(params, msgId);
		}else if(MsgType.CLOSEUSER.toString().equals(msgType)){
			return UserValid.validClose(params, msgId);
		}else if(MsgType.BINDACCOUNT.toString().equals(msgType)){
			return AccountValid.validBind(params, msgId);
		}else if(MsgType.UNBINDACCOUNT.toString().equals(msgType)){
			return AccountValid.validUnbind(params, msgId);
		}else if(MsgType.ACTIVATECARD.toString().equals(msgType)){
			return AccountValid.validActivateCard(params, msgId);
		}else if(MsgType.CARDBLACKLIST.toString().equals(msgType)){
			return AccountValid.validCardBlackList(params, msgId);
		}else if(MsgType.UPDATEPAYPASSWD.toString().equals(msgType)){
			return AccountValid.validUpdatePasswd(params, msgId);
		}else if(MsgType.LOSTCARD.toString().equals(msgType)){
			return AccountValid.validLostCard(params, msgId);
		}else if (MsgType.BINDDEVICE.toString().equals(msgType)){
			return DeviceValid.validBind(params, msgId);
		}else if (MsgType.UNBINDDEVICE.toString().equals(msgType)){
			return DeviceValid.validUnbind(params, msgId);
		}else if (MsgType.RECHARGE.toString().equals(msgType)) {
			return PayValid.validRecharge(params, msgId);
		} else {
			return failMsg(result,"没有匹配的消息类型！",ResponseType.NOT_MATCH_TYPE.toString(),msgId);
		}
	}
}
